#-*- coding:utf-8 -*-
"""
author:tuhou
time:2021/7/24 上午10:24 
思路：
"""
from PIL import Image
import io
import requests


class get_slide:
    def get_merge_image(self,imurl):  # 传入带缺口信息的图片的地址
        # [39, 38, 48, 49, 41, 40, 46, 47, 35, 34, 50, 51, 33, 32, 28, 29, 27, 26, 36, 37, 31, 30, 44, 45, 43, 42,
        #  12, 13, 23, 22, 14, 15, 21, 20, 8, 9, 25, 24, 6, 7, 3, 2, 0, 1, 11, 10, 4, 5, 19, 18, 16, 17]
        # 此处的x取10
        location_list = [{"x": -157, "y": -80}, {"x": -145, "y": -80}, {"x": -265, "y": -80}, {"x": -277, "y": -80},
                         {"x": -181, "y": -80}, {"x": -169, "y": -80}, {"x": -241, "y": -80}, {"x": -253, "y": -80},
                         {"x": -109, "y": -80}, {"x": -97, "y": -80}, {"x": -289, "y": -80}, {"x": -301, "y": -80},
                         {"x": -85, "y": -80}, {"x": -73, "y": -80}, {"x": -25, "y": -80}, {"x": -37, "y": -80},
                         {"x": -13, "y": -80}, {"x": -1, "y": -80}, {"x": -121, "y": -80}, {"x": -133, "y": -80},
                         {"x": -61, "y": -80}, {"x": -49, "y": -80}, {"x": -217, "y": -80}, {"x": -229, "y": -80},
                         {"x": -205, "y": -80}, {"x": -193, "y": -80},
                         {"x": -145, "y": 0}, {"x": -157, "y": 0},
                         {"x": -277, "y": 0}, {"x": -265, "y": 0}, {"x": -169, "y": 0}, {"x": -181, "y": 0},
                         {"x": -253, "y": 0}, {"x": -241, "y": 0}, {"x": -97, "y": 0}, {"x": -109, "y": 0},
                         {"x": -301, "y": 0}, {"x": -289, "y": 0}, {"x": -73, "y": 0}, {"x": -85, "y": 0},
                         {"x": -37, "y": 0}, {"x": -25, "y": 0}, {"x": -1, "y": 0}, {"x": -13, "y": 0},
                         {"x": -133, "y": 0}, {"x": -121, "y": 0}, {"x": -49, "y": 0}, {"x": -61, "y": 0},
                         {"x": -229, "y": 0}, {"x": -217, "y": 0}, {"x": -193, "y": 0}, {"x": -205, "y": 0}]

        bg_img = requests.get(imurl, timeout=3).content
        bg_img = io.BytesIO(bg_img)

        im = Image.open(bg_img)

        im_list_upper = []
        im_list_down = []
        for location in location_list:
            if location['y'] == -80:
                im_list_upper.append(
                    im.crop((abs(location['x']), 80, abs(location['x']) + 10, 160)))  # 有关参数的介绍请看下方的crop方法的介绍， 可自行调整
            if location['y'] == 0:
                im_list_down.append(im.crop((abs(location['x']), 0, abs(location['x']) + 10, 0 + 80)))

        new_im = Image.new('RGB', (260, 160))  # 生成的图片的大小， 可调整

        x_offset = 0
        for im in im_list_upper:
            new_im.paste(im, (x_offset, 0))
            x_offset += im.size[0]

        x_offset = 0
        for im in im_list_down:
            new_im.paste(im, (x_offset, 80))
            x_offset += im.size[0]

        return new_im
        # 将重组之后的图片进行返回 也可直接使用 new_im.save("./captcha.png") 保存

    def is_px_equal(self, img1, img2, x, y):
        """
        判断两个像素是否相同
        :param img1: 图片1
        :param img2:图片2
        :param x:位置1
        :param y:位置2
        :return:像素是否相同
        """
        pix1 = img1.load()[x, y]
        pix2 = img2.load()[x, y]
        threshold = 60

        if abs(pix1[0] - pix2[0]) < threshold and abs(pix1[1] -pix2[1]) < threshold and abs(pix1[2] - pix2[2]) < threshold:
            return True
        else:
            return False

    def get_gap(self, img1, img2):
        """
        获取缺口偏移量
        :param img1: 不带缺口图片
        :param img2: 带缺口图片
        :return:将原本的改为传路径
        要读取图片的大小等,需要使用Image.open
        """
        img1 = Image.open(img1)
        img2 = Image.open(img2)
        left = 0
        for row in range(left, img1.size[0]):
            for col in range(img1.size[1]):
                if not self.is_px_equal(img1, img2, row, col):
                    left = row
                    return left
        return left

xx = get_slide()
# 完整图
url1 = 'https://static.geetest.com/pictures/gt/09b7341fb/09b7341fb.jpg'
# 缺口图
url2 = 'https://static.geetest.com/pictures/gt/09b7341fb/bg/81cca7b6d.jpg'
xx.get_merge_image(url1).save('img1.png')
xx.get_merge_image(url2).save('img2.png')
left = xx.get_gap('img1.png','img2.png')
print(left)